24 #define foreach(x, v) for (typeof (v).begin() x=(v).begin(); x !=(v).end(); ++x)
25 #define For(i, a, b) for (int i=(a); i<(b); ++i)
26 #define D(x) cout << #x " is " << x << endl
33 enum { white
, gray
, black
};
35 map
<string
, int> face
;
38 int getIndex(const string
&s
) {
39 if (face
.count(s
) > 0) return face
[s
];
40 return face
[s
] = currentFace
++;
44 memset(g
, 0, sizeof g
);
49 bool cycleFrom(int u
) {
50 if (color
[u
] == gray
) return true;
51 if (color
[u
] == black
) return false;
53 for (int v
= 0; v
< nodes
; ++v
) {
54 if (!g
[u
][v
]) continue;
55 if (cycleFrom(v
)) return true;
62 for (int i
= 0; i
< nodes
; ++i
) color
[i
] = white
;
63 for (int i
= 0; i
< nodes
; ++i
) {
64 if (cycleFrom(i
)) return true;
75 for (int u
= 0; u
< nodes
; ++u
) {
76 for (int v
= 0; v
< nodes
; ++v
) {
93 while (cin
>> alice
>> bob
) {
94 if (alice
== "*" and bob
== "*") break;
97 vector
< vector
<string
> > a(1);
101 a
.push_back(vector
<string
>());
104 a
.back().push_back(s
);
107 vector
< vector
<string
> > b(1);
111 b
.push_back(vector
<string
>());
114 b
.back().push_back(s
);
119 // for (int i = 0; i < a.size(); ++i) {
120 // printf("row[%d] has %d items\n", i, a[i].size());
121 // for (int j = 0; j < a[i].size(); ++j) {
122 // printf("%s ", a[i][j].c_str());
127 // for (int i = 0; i < b.size(); ++i) {
128 // printf("row[%d] has %d items\n", i, b[i].size());
129 // for (int j = 0; j < b[i].size(); ++j) {
130 // printf("%s ", b[i][j].c_str());
135 for (int i
= 0; i
< a
.size(); ++i
) {
136 for (int j
= 1; j
< a
[i
].size(); ++j
) {
137 g
[ getIndex(a
[i
][j
-1]) ][ getIndex(a
[i
][j
]) ] = true;
141 for (int i
= 0; i
< b
.size(); ++i
) {
142 for (int j
= 1; j
< b
[i
].size(); ++j
) {
143 g
[ getIndex(b
[i
][j
-1]) ][ getIndex(b
[i
][j
]) ] = true;
148 // for (int i = 0; i < nodes; ++i) {
149 // for (int j = 0; j < nodes; ++j) {
150 // printf("%d ", g[i][j]);